﻿using System;
using BruteForce.Animation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Bruteforce.Animation
{
    public class ArrowTransition : MapDrawableTransition
    {
        public static Texture2D ArrowTexture;

        public Vector2 EndPosition;
        public Vector2 StartPosition;
        private Vector2 _nsteps;

        public ArrowTransition(double duration) : base(duration)
        {
        }

        //b = 0, c = 1 , t = Time, d = Duration.

        public override float TextureScale
        {
            get
            {
                if (Value < 0.5)
                {
                    return (float) (0.2*Value*2);
                }
                else
                {
                    return (float) (0.2*(2 - 2*Value));
                }
                //return 0.1f;
            }
        }

        public override float Rotation
        {
            get { return (float) (Math.Atan2((EndPosition.Y - StartPosition.Y), (EndPosition.X - StartPosition.X))); }
        }


        public override Texture2D Texture
        {
            get { return ArrowTexture; }
        }

        public override Vector2 WorldLocation
        {
            get
            {
                return new Vector2((float) (StartPosition.X + _nsteps.X*Value),
                                   (float) (StartPosition.Y + _nsteps.Y*Value));
            }
        }

        public override void Interpolate(GameTime time)
        {
            double t = Time;
            double d = Duration;
            int b = 0;
            int c = 1;
            double ts = (t /= d)*t;
            double tc = ts*t;
            Value = b + c*(4.3*tc*ts + -12.9*ts*ts + 12.9*tc + -4.3*ts + t);
        }

        public override void StartTransition()
        {
            _nsteps = new Vector2(EndPosition.X - StartPosition.X, EndPosition.Y - StartPosition.Y);
            base.StartTransition();
        }
    }
}